#--------------------------------#
# Creating the network diffusion #
#--------------------------------#
rm(list = ls())



range01 <- function(x){(x-min(x, na.rm = T))/(max(x, na.rm = T)-min(x, na.rm = T))}
## Load in imputed
all.data <- readRDS("data/data_for_analysis_replication.rds")
all.data$year <- as.numeric(as.character(all.data$year))
adjacency_matricies <- readRDS("intl_order/adjacency_matricies_replication.rds")

## Load in other data
qog               <- haven::read_dta("data/qog_std_ts_jan20.dta")
full.data         <- get({data(cow_nmc)})
civil_cow         <- haven::read_dta("data/intra_state_wars_cow.dta")
state.names       <- read.csv("data/states2016.csv")
intl_groups       <- readRDS("intl_order/international_order_replication.rds")
network_layers    <- readRDS("intl_order/network_layers_replication.rds")
unweighted_data   <- readRDS("intl_order/unweighted_data_replication.rds")

## Initial data formatting
all.data$polity <- as.numeric(as.character(all.data$polity))
min_year <- 1815

## Format the data
# GDP
qog_democ_spread <- dplyr::select(
  qog, year, ccodecow, gle_cgdpc, wdi_gdpcapgr, gle_gdp
)

# Contiguity
contiguity_temp <- distinct(
  unweighted_data,
  cowc1, cowc2, year, contig
)
 
contiguity <- distinct(
  unweighted_data,
  cowc1, cowc2, year, contig
) %>% mutate(
  ccode1a = cowc2, 
  ccode2a = cowc1, 
  cowc1 = ccode1a,
  cowc2 = ccode2a
) %>% dplyr::select(
  cowc1, cowc2, year, contig
) %>% bind_rows(
  contiguity_temp
)
  
# Format civil war data
civil_war_data <- list()
civil_cow <- dplyr::select(
  civil_cow,
  c(
    CcodeA, StartYr1, EndYr1, StartYr2, EndYr2, StartYr3, EndYr3, StartYr4, EndYr4
  )
) %>% 
  filter(
    !is.na(CcodeA),
    !is.na(StartYr1),
    CcodeA != -8
  )

## Create yearly civil war data
for (i in 1:nrow(civil_cow))
{
  temp_data <- civil_cow[i,]
  temp_data <- temp_data[,colSums(temp_data == -8) != 1]
  temp_data[temp_data == -7] <- 2014
  year_range <- list()
  for (z in 1:sum(grepl("StartYr", colnames(temp_data))))
  {
    
    temp_years <- as.numeric(temp_data[1,paste0("StartYr", z)]):as.numeric(temp_data[1,paste0("EndYr", z)])
    year_range[[z]] <- data.frame(name = temp_data$CcodeA[1], year = temp_years)
  }
  civil_war_data[[i]] <- do.call(bind_rows, year_range)
}

civil_war_data <- do.call(bind_rows, civil_war_data)
civil_war_data$civil_war <- 1


## Rename variable for merge
all.data <- dplyr::rename(
  all.data, 
  membership = id
)


## Merge data
all.data <- left_join(
  all.data,
    qog_democ_spread, 
    by = c(
      "year", 
      "ccode" = "ccodecow"
    ),
  multiple = "all"
) %>% left_join(
    civil_war_data,
    by = c(
      "year", 
      "ccode" = "name"
    ),
    multiple = "all"
  ) %>% mutate(
    civil_war = ifelse(is.na(civil_war), 0, civil_war)
  )


## Create diffusion data
polity.diffusion <- list()
for (j in 1:length(adjacency_matricies))
{
  
  ## Extract network
  g <- graph_from_adjacency_matrix(adjacency_matricies[[j]], mode = "directed", weighted = T)
  
  ## Weighted tie strength
  indeg <- data.frame(
    names = names(degree(g)),
    indegree = strength(g, mode = "in")
  )
  
  ## Turn into edge list
  weighted_edges <- reshape2::melt(adjacency_matricies[[j]])
  
  weighted_edges <- filter(
    weighted_edges, 
    Var1 != Var2 ## Drop self loops
  ) %>% dplyr::rename(
    from = Var1, ## Sender
    to   = Var2, ## Reciever
    tie = value  ## Edge strength
  )
  
  ## Subset data
  democ_autoc_data <- filter(
    all.data, 
    year == j + min_year
  ) %>% 
    distinct(
      names, year, .keep_all = T
    )
  
  ## Make sure it is unique
  power_temp <- distinct(
      democ_autoc_data,
      names, cinc, gle_gdp
    )
  
  ## Merge in power
  edge.indeg <- left_join(
    weighted_edges,
      power_temp,
      by = c("from" = "names")
    ) %>% left_join(
      power_temp,
      by = c("to" = "names")
    )  %>% mutate(
      power_flag = ifelse(cinc.x > cinc.y, 1, 0),
      democ_flag = ifelse(from %in% subset(democ_autoc_data, polity >= 6)$names, 1, 0),
      democ_power_flag = ifelse(from %in% subset(democ_autoc_data, polity >= 6)$names & cinc.x > cinc.y, 1, 0),
      democ_flag_gdp = ifelse(from %in% subset(democ_autoc_data, polity >= 6)$names & gle_gdp.x > gle_gdp.y, 1, 0),
      democ_flag_vdem = ifelse(from %in% subset(democ_autoc_data, v2x_polyarchy >= 0.5)$names, 1, 0),
      democ_power_flag_vdem = ifelse(from %in% subset(democ_autoc_data, v2x_polyarchy >= 0.5)$names & cinc.x > cinc.y, 1, 0),
      autoc_flag = ifelse(from %in% subset(democ_autoc_data, polity <= -6)$names, 1, 0),
      autoc_power_flag = ifelse(from %in% subset(democ_autoc_data, polity <= -6)$names & cinc.x > cinc.y, 1, 0),
      autoc_flag_gdp = ifelse(from %in% subset(democ_autoc_data, polity <= -6)$names & gle_gdp.x > gle_gdp.y, 1, 0),
      exec_constraint = ifelse(from %in% subset(democ_autoc_data,  xconst  == 7)$names & cinc.x > cinc.y, 1, 0),
      exec_constraint_gdp = ifelse(from %in% subset(democ_autoc_data,  xconst  == 7)$names & gle_gdp.x > gle_gdp.y, 1, 0),
      comp_pol_part = ifelse(from %in% subset(democ_autoc_data,  parcomp  == 5)$names & cinc.x > cinc.y, 1, 0),
      unli_exec = ifelse(from %in% subset(democ_autoc_data,  xconst  == 1)$names & cinc.x > cinc.y, 1, 0),
      rest_pol_part = ifelse(from %in% subset(democ_autoc_data,  parreg  == 4)$names & cinc.x > cinc.y, 1, 0),
      rest_pol_part_gdp = ifelse(from %in% subset(democ_autoc_data,  parreg  == 4)$names & gle_gdp.x > gle_gdp.y, 1, 0),
      women_participation_flag = ifelse(from %in% subset(democ_autoc_data, v2csgender_ord >= 3)$names & cinc.x > cinc.y, 1, 0),
      gov_censorship_flag = ifelse(from %in% subset(democ_autoc_data, v2mecenefm_ord >= 3)$names & cinc.x > cinc.y, 1, 0),
      gov_censorship_flag_gdp = ifelse(from %in% subset(democ_autoc_data, v2mecenefm_ord >= 3)$names & gle_gdp.x > gle_gdp.y, 1, 0),
      religious_freedom_flag = ifelse(from %in% subset(democ_autoc_data, v2csrlgrep_ord >= 3)$names & cinc.x > cinc.y, 1, 0)
    ) %>% plyr::ddply(
      ~to,
      summarize, 
      indeg_power = sum(power_flag * tie),
      indeg_democracy = sum(democ_flag * tie),
      indeg_democ_power_int = sum(democ_power_flag  * tie),
      indeg_democ_power_gdp = sum(democ_flag_gdp  * tie, na.rm = T),
      indeg_democracy_vdem = sum(democ_flag_vdem  * tie),
      indeg_democ_power_int_vdem = sum(democ_power_flag_vdem * tie),
      indeg_women_participation = sum(women_participation_flag * tie),
      indeg_autoc_flag = sum(autoc_flag * tie),
      indeg_autoc_power_flag = sum(autoc_power_flag * tie),
      indeg_autoc_power_gdp = sum(autoc_flag_gdp * tie, na.rm = T),
      indeg_exec_constraint = sum(exec_constraint * tie),
      indeg_exec_constraint_gdp = sum(exec_constraint_gdp * tie, na.rm = T),
      indeg_comp_pol_part = sum(comp_pol_part * tie),
      indeg_unli_exec = sum(unli_exec * tie),
      indeg_rest_pol_part = sum(rest_pol_part * tie),
      indeg_rest_pol_part_gdp = sum(rest_pol_part_gdp * tie, na.rm = T),
      indeg_gov_censorship = sum(gov_censorship_flag * tie),
      indeg_gov_censorship_gdp = sum(gov_censorship_flag_gdp * tie, na.rm = T),
      indeg_religious_freedom = sum(religious_freedom_flag * tie)
    ) 
  
  edge.indeg <- edge.indeg[!duplicated(edge.indeg),]
  
  ## Find neighbors with attribute
  neighbor.status <- filter(
    contiguity,
      year == j + min_year,
      contig == 1
    ) %>% 
    rename(
      from = cowc1,
      to = cowc2
    )
  
  ## Create neighbor with attribute variable
  neighbor.status <- mutate(
    neighbor.status,
      n = 1,
      democratic_neighbor = ifelse(from %in% subset(democ_autoc_data,  polity >= 6)$names, 1, 0),
      democratic_neighbor_vdem = ifelse(from %in% subset(democ_autoc_data,  v2x_polyarchy >= 0.5)$names, 1, 0),
      autocratic_neighbor = ifelse(from %in% subset(democ_autoc_data,  polity <= -6)$names, 1, 0),
      exec_constraint_neighbor = ifelse(from %in% subset(democ_autoc_data,  xconst  == 7)$names, 1, 0),
      comp_pol_part_neighbor = ifelse(from %in% subset(democ_autoc_data,  parcomp  == 5)$names, 1, 0),
      unli_exec_neighbor = ifelse(from %in% subset(democ_autoc_data,  xconst  == 1)$names, 1, 0),
      rest_pol_part_neighbor = ifelse(from %in% subset(democ_autoc_data,  parreg  == 4)$names, 1, 0),
      women_participation_neighbor = ifelse(from %in% subset(democ_autoc_data, v2csgender_ord >= 3)$names, 1, 0),
      gov_censorship_neighbor = ifelse(from %in% subset(democ_autoc_data, v2mecenefm_ord >= 3)$names, 1, 0),
      religious_freedom_neighbor = ifelse(from %in% subset(democ_autoc_data, v2csrlgrep_ord >= 3)$names, 1, 0)
    ) %>% 
    plyr::ddply(
      ~to, 
      summarize, 
      democratic_neighbor = sum(democratic_neighbor)/sum(n),
      autocratic_neighbor = sum(autocratic_neighbor)/sum(n),
      democratic_neighbor_vdem = sum(democratic_neighbor_vdem)/sum(n),
      exec_constraint_neighbor = sum(exec_constraint_neighbor)/sum(n),
      comp_pol_part_neighbor = sum(comp_pol_part_neighbor)/sum(n),
      unli_exec_neighbor = sum(unli_exec_neighbor)/sum(n),
      rest_pol_part_neighbor = sum(rest_pol_part_neighbor)/sum(n),
      women_participation_neighbor = sum(women_participation_neighbor)/sum(n),
      gov_censorship_neighbor = sum(gov_censorship_neighbor)/sum(n),
      religious_freedom_neighbor = sum(religious_freedom_neighbor)/sum(n),
      democratic_neighbor = tidyr::replace_na(democratic_neighbor, 0),
      autocratic_neighbor = tidyr::replace_na(autocratic_neighbor, 0),
      democratic_neighbor_vdem = tidyr::replace_na(democratic_neighbor_vdem, 0),
      exec_constraint_neighbor = tidyr::replace_na(exec_constraint_neighbor, 0),
      comp_pol_part_neighbor = tidyr::replace_na(comp_pol_part_neighbor, 0),
      unli_exec_neighbor = tidyr::replace_na(unli_exec_neighbor, 0),
      rest_pol_part_neighbor = tidyr::replace_na(rest_pol_part_neighbor, 0),
      women_participation_neighbor = tidyr::replace_na(women_participation_neighbor, 0)
    )
  
  ## Join the data together
  suppressWarnings(year.data <- left_join(
    indeg,
    edge.indeg, 
      by = c(
        "names" = "to"
      )                                      
    ) %>% left_join(
    neighbor.status,
      by = c(
        "names" = "to"
      )
    ) %>% left_join(
      democ_autoc_data,
      by = c("names")
    )  %>% mutate(
      indeg_democracy = tidyr::replace_na(indeg_democracy, 0),
      indeg_democ_power_int = tidyr::replace_na(indeg_democ_power_int, 0),
      indeg_democracy_vdem = tidyr::replace_na(indeg_democracy_vdem, 0),
      indeg_democ_power_gdp = tidyr::replace_na(indeg_democ_power_gdp, 0),
      indeg_autoc_power_gdp = tidyr::replace_na(indeg_autoc_power_gdp, 0),
      indeg_democ_power_int_vdem = tidyr::replace_na(indeg_democ_power_int_vdem, 0),
      indeg_women_participation = tidyr::replace_na(indeg_women_participation, 0),
      indeg_autoc_flag = tidyr::replace_na(indeg_autoc_flag, 0), 
      indeg_autoc_power_flag = tidyr::replace_na(indeg_autoc_power_flag, 0), 
      indeg_exec_constraint = tidyr::replace_na(indeg_exec_constraint, 0), 
      indeg_exec_constraint_gdp = tidyr::replace_na(indeg_exec_constraint_gdp, 0), 
      indeg_comp_pol_part = tidyr::replace_na(indeg_comp_pol_part, 0), 
      indeg_unli_exec = tidyr::replace_na(indeg_unli_exec, 0), 
      indeg_rest_pol_part = tidyr::replace_na(indeg_rest_pol_part, 0),
      indeg_rest_pol_part_gdp = tidyr::replace_na(indeg_rest_pol_part_gdp, 0),
      indeg_gov_censorship = tidyr::replace_na(indeg_gov_censorship, 0),
      indeg_gov_censorship_gdp = tidyr::replace_na(indeg_gov_censorship_gdp, 0),
      indeg_religious_freedom = tidyr::replace_na(indeg_religious_freedom, 0),
      democ_temp = ifelse(polity >= 6, 1, 0),
      democratic_neighbor = tidyr::replace_na(democratic_neighbor, 0),
      autocratic_neighbor = tidyr::replace_na(autocratic_neighbor, 0),
      rest_pol_part_neighbor = tidyr::replace_na(rest_pol_part_neighbor, 0),
      women_participation_neighbor = tidyr::replace_na(women_participation_neighbor, 0),
      gov_censorship_neighbor = tidyr::replace_na(gov_censorship_neighbor, 0),
      exec_constraint_neighbor = tidyr::replace_na(exec_constraint_neighbor, 0),
      religious_freedom_neighbor = tidyr::replace_na(religious_freedom_neighbor, 0)
    )
  )  
  
  ## Fix missing data
  suppressWarnings(year.data <- mutate(
    year.data,
    indegree = tidyr::replace_na(indegree, 0),
    prop.democ = indeg_democracy/indegree,
    prop.democ.mimic = indeg_democ_power_int/indegree,
    prop.democ.mimic.gdp = indeg_democ_power_gdp/indegree,
    prop.autoc.mimic.gdp = indeg_autoc_power_gdp/indegree,
    prop.women.suffrage.mimic = indeg_women_participation/indegree,
    prop.gov_censorship.mimic = indeg_gov_censorship/indegree,
    prop.gov_censorship.mimic.gpd = indeg_gov_censorship_gdp/indegree,
    prop.religious.freedom.mimic = indeg_religious_freedom/indegree,
    prop.democ.mimic.vdem = indeg_democracy_vdem/indegree,
    prop.autoc = indeg_autoc_flag/indegree,
    prop.autoc.mimic = indeg_autoc_power_flag/indegree,
    prop.exec_constraint = indeg_exec_constraint/indegree,
    prop.exec_constraint_gdp = indeg_exec_constraint_gdp/indegree,
    prop.comp_pol_part = indeg_comp_pol_part/indegree,
    prop.unli_exec = indeg_unli_exec/indegree,
    prop.rest_pol_part = indeg_rest_pol_part/indegree,
    prop.rest_pol_part_gdp = indeg_rest_pol_part_gdp/indegree,
    prop.democ = tidyr::replace_na(prop.democ, 0),
    prop.democ.mimic = tidyr::replace_na(prop.democ.mimic, 0),
    prop.women.suffrage.mimic = tidyr::replace_na(prop.women.suffrage.mimic, 0),
    prop.gov_censorship.mimic = tidyr::replace_na(prop.gov_censorship.mimic, 0),
    prop.religious.freedom.mimic = tidyr::replace_na(prop.religious.freedom.mimic, 0),
    prop.democ.mimic.vdem = tidyr::replace_na(prop.democ.mimic.vdem, 0),
    prop.autoc = tidyr::replace_na(prop.autoc, 0),
    prop.autoc.mimic = tidyr::replace_na(prop.autoc.mimic, 0),
    prop.exec_constraint = tidyr::replace_na(prop.exec_constraint, 0),
    prop.comp_pol_part = tidyr::replace_na(prop.comp_pol_part, 0),
    prop.unli_exec = tidyr::replace_na(prop.unli_exec, 0),
    prop.rest_pol_part = tidyr::replace_na(prop.rest_pol_part, 0)
  ) 
  )
  colnames(year.data) <- gsub("\\.", "_", colnames(year.data))
  colnames(year.data) <- trimws(colnames(year.data))
  
  if (all(is.na(year.data$gle_gdp)))
  {
    year.data[,substr(colnames(year.data), nchar(colnames(year.data)) - 3, nchar(colnames(year.data))) == "_gdp"] <- NA
  }
  
  if (all(is.na(year.data$v2mecenefm_ord)))
  {
    year.data$prop_gov_censorship_mimic <- NA
  }
  
  if (all(is.na(year.data$v2csrlgrep_ord)))
  {
      year.data$prop_religious_freedom_mimic <- NA
  }
  
  if (all(is.na(year.data$v2x_polyarchy)))
  {
    year.data$prop_democ_mimic_vdem <- NA
  }
  
  if (all(is.na(year.data$v2csgender_ord)))
  {
    year.data$prop_women_suffrage_mimic <- NA
  }
  
  
  polity.diffusion[[j]] <- year.data
}

for (i in 1:length(polity.diffusion))
{
  if(any(colnames(polity.diffusion[[i]]) == "year_x"))
  {
    stop("Problem in the code")
  }
}

## Bring together data
polity_diffusion <- do.call(bind_rows, polity.diffusion)

## Fix column names
colnames(polity_diffusion) <- gsub("\\.", "_", colnames(polity_diffusion))

## Save data
saveRDS(polity_diffusion, file = "data/polity_diffusion_replication.rds")

